package com.kgc.scd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.scd.bean.*;
import com.kgc.scd.mapper.CartMapper;
import com.kgc.scd.mapper.ProductMapperJzp;
import com.kgc.scd.service.BlCartService;
import com.kgc.scd.util.PageSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created On : 2022/6/10.
 * <p>
 * Author     : 98730
 * <p>
 * Description: 购物车功能实现类
 */
@Slf4j
@Service
public class BlCartServiceImpl implements BlCartService {

    @Autowired(required = false)
    private CartMapper cartMapper;

    @Autowired(required = false)
    private ProductMapperJzp productMapperJzp;

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [com.kgc.scd.bean.Cart]
     * @return : boolean
     * @description : 添加购物车
     */
    @Override
    public boolean addCartByProId(Cart cart) {
        return cartMapper.insertSelective(cart) > 0;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.util.List<java.lang.Integer>, java.lang.String]
     * @return : boolean
     * @description : 删除购物车列表
     */
    @Override
    public boolean delCartById(List<Integer> cartId,String userId) {
        for(Integer id : cartId){
            CartExample cartExample = new CartExample();
            CartExample.Criteria criteria = cartExample.createCriteria();
            criteria.andCartIdEqualTo(id).andCartUidEqualTo(userId);
            cartMapper.deleteByExample(cartExample);
        }
        return true;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.Integer, java.lang.Integer, java.lang.String]
     * @return : com.kgc.scd.util.PageSupport<com.kgc.scd.bean.CartItem>
     * @description : 根据用户id查询用户购物车列表，支持分页操作
     */
    @Override
    public PageSupport<CartItem> queryCartListByPage(Integer pageNo, Integer pageSize, String userId) {
        // 分页
        PageHelper.startPage(pageNo,pageSize);
        List<CartItem> cartItems = cartMapper.selectDetailByUserId(userId);

        PageInfo<CartItem> pageInfo = new PageInfo<>(cartItems);
        PageSupport<CartItem> pageSupport = new PageSupport<>();
        // 设置分页内容
        pageSupport.setPageData(cartItems);
        // 设置当前页码
        pageSupport.setPageNo(pageNo);
        // 设置每页条数
        pageSupport.setPageSize(pageSize);
        // 设置总页数
        pageSupport.setTotalPage(pageInfo.getPages());
        // 设置总记录数
        pageSupport.setTotalCount((int) pageInfo.getTotal());
        return pageSupport;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.String]
     * @return : com.kgc.scd.bean.Product
     * @description : 查询商品库存数
     */
    @Override
    public Product queryProStock(String proId) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andProIdEqualTo(proId);

        return productMapperJzp.selectByExample(productExample).get(0);
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.String]
     * @return : java.util.List<com.kgc.scd.bean.Cart>
     * @description : 查询购物车列表
     */
    @Override
    public List<Cart> queryCartList(String userId) {
        return cartMapper.selectDetailReturnCart(userId);
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [com.kgc.scd.bean.Cart]
     * @return : boolean
     * @description : 更新购物车列表
     */
    @Override
    public boolean updateCartList(Cart cart) {
        return cartMapper.updateByPrimaryKeySelective(cart) > 0;
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.String, java.lang.String]
     * @return : com.kgc.scd.bean.Cart
     * @description : 根据用户查询购物车
     */
    @Override
    public Cart queryCartListByUserId(String proId,String userId) {
        CartExample cartExample = new CartExample();
        CartExample.Criteria criteria = cartExample.createCriteria();
        criteria.andCartProidEqualTo(proId).andCartUidEqualTo(userId);
        return cartMapper.selectByExample(cartExample).get(0);
    }

}
